package org.yate.oa.utils;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.codec.digest.DigestUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class MessageDigestUtils {

	public static String toBase64String(String datas, String charset) {
		return new BASE64Encoder().encode(datas.getBytes(Charset.forName(charset)));
	}

	public static String toBase64String(String datas) {
		return toBase64String(datas, "UTF-8");
	}

	public static String decodeBase64String(String datas, String charset) {
		try {
			return new String(new BASE64Decoder().decodeBuffer(datas), Charset.forName(charset));
		} catch (IOException e) {
			return null;
		}
	}

	public static String decodeBase64String(String datas) {
		return decodeBase64String(datas, "UTF-8");
	}

	public static String toMD5Hex(String datas) {
		return toMD5Hex(datas, "UTF-8");
	}

	public static String toMD5Hex(String datas, String charset) {
		return toMD5Hex(datas.getBytes(Charset.forName(charset)));
	}

	public static String toMD5Hex(byte[] datas) {
		return DigestUtils.md5Hex(datas);
	}
	
	public static void main(String[] args){
		System.out.println(toMD5Hex("123456"));
	}
	
}
